package UnitTest;

/**
 * @Auther: Administrator
 * @Date: 2019/6/14/014 21 17
 * @Description:
 */

class Node{
    Node next;
    int data;
    public Node(int data) {
        this.data = data;
    }
    public Node getNext() {
        return next;
    }
    public void setNext(Node next) {
        this.next = next;
    }
}
public class  Solution {
    public static Node reverse(Node node) {
        //单向链表的该节点为空 ，或者该节点对下一节点的引用为空时退出递归
        if(node == null || node.next == null) {
            return node;
        }
        //（以当前节点为坐标点）翻转该节点对下一节点的引用 【将下一节点引用指向该节点，将下下一节点的引用指向下一节点 此步递归操作】
        Node newHead = reverse(node.next);
        //当前节点的下一个节点的节点引用指向当前节点，当前节点对下一个节点的引用被置空
        node.next.next = node;
        node.next = null;
        return newHead;
    }
    public static void printNode(Node node) {
        System.out.println(node.data);
        if(node.getNext() != null) {
            printNode(node.getNext());
        }
    }
    public static void main(String[] args) {
        Node q1 = new Node(1);
        Node q2 = new Node(2);
        Node q3 = new Node(3);
        Node q4 = new Node(4);
        Node q5 = new Node(5);
        q1.setNext(q2);
        q2.setNext(q3);
        q3.setNext(q4);
        q4.setNext(q5);
        printNode(q1);
        System.out.println("--------------------");
        Node q = reverse(q1);
        printNode(q);
    }
}
